package com.yuyang.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.yuyang.gulimall.product.entity.AttrEntity;
import com.yuyang.gulimall.product.service.CategoryService;
import com.yuyang.gulimall.product.vo.AttrGroupRelationVo;
import com.yuyang.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.yuyang.gulimall.product.entity.AttrGroupEntity;
import com.yuyang.gulimall.product.service.AttrGroupService;
import com.yuyang.common.utils.PageUtils;
import com.yuyang.common.utils.R;



/**
 * 属性分组
 *
 * @author yuyang
 * @email yuyang@gmail.com
 * @date 2022-11-21 22:31:38
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;


    /**
     * 获取分类下所有分组&关联属性
     */
//    /product/attrgroup/{catelogId}/withattr
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttr(@PathVariable("catelogId") Long catelogId){
        List<AttrGroupWithAttrsVo> attrGroupWithAttrsVos = attrGroupService.getAttrGroupWithAttrByCatelogId(catelogId);

        return R.ok().put("data",attrGroupWithAttrsVos);
    }

    /**
     * 获取属性分组的关联的所有属性
     */
    @GetMapping("/{attrGroupId}/attr/relation")
    public R getAttrRelation(@PathVariable("attrGroupId") Long attrGroupId){
        List<AttrEntity> attrEntityList = attrGroupService.getAttrRelation(attrGroupId);
        return R.ok().put("data",attrEntityList);
    }

    /**
     * 删除属性与分组的关联关系
     */

    @PostMapping("/attr/relation/delete")
    public R deleteAttrRelated(@RequestBody AttrGroupRelationVo[] attrGroupRelationVos){

        attrGroupService.deleteAttrRelation(attrGroupRelationVos);
        return R.ok();
    }

    /**
     * 添加属性与分组关联关系
     */
    @PostMapping("/attr/relation")
    public R saveAttrRelated(@RequestBody List<AttrGroupRelationVo> attrGroupRelationVos){
        attrGroupService.saveAttrRelated(attrGroupRelationVos);
        return R.ok();
    }
    /**
     * 获取属性分组没有关联的其他属性
     */
//    /product/attrgroup/{attrgroupId}/noattr/relation
    @GetMapping("/{attrGroupId}/noattr/relation")
    public R getNoAttrRelated(@RequestParam Map<String, Object> params,@PathVariable("attrGroupId") Long attrGroupId){
        PageUtils page = attrGroupService.getNoAttrRelated(params, attrGroupId);
        return R.ok().put("page",page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("catelogId") Long catelogId){
        PageUtils page = attrGroupService.queryPage(params,catelogId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long catelogId = attrGroup.getCatelogId();
        attrGroup.setCatelogPath(categoryService.findCatelogPath(catelogId));

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
